package org.easyway.tiles.generic;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class TypeSelector extends JDialog {

	private static final long serialVersionUID = -1705890998901610166L;

	private JPanel container = null;

	private PImage pimage = null;

	private JLabel ltype = null;

	private JTextField type = null;

	private JButton ok = null;

	private JButton cancel = null;

	public TextureButton tile;

	/**
	 * This is the default constructor
	 */
	public TypeSelector(JFrame owner, TextureButton tile) {
		super(owner, true);
		this.tile = tile;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(348, 151);
		this.setTitle("Set the Type of Texture");
		this.setContentPane(getContainer());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getContainer() {
		if (container == null) {
			ltype = new JLabel();
			ltype.setBounds(new java.awt.Rectangle(108, 27, 38, 16));
			ltype.setText("Type:");
			container = new JPanel();
			container.setLayout(null);
			container.add(getImage(), null);
			container.add(ltype, null);
			container.add(getType(), null);
			container.add(getOk(), null);
			container.add(getCancel(), null);
		}
		return container;
	}

	/**
	 * This method initializes image
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getImage() {
		if (pimage == null) {
			pimage = new PImage();
			pimage.setSize(new java.awt.Dimension(64, 64));
			pimage.setPreferredSize(new java.awt.Dimension(64, 64));
			pimage.setLocation(new java.awt.Point(20, 20));
		}
		return pimage;
	}

	/**
	 * This method initializes type
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getType() {
		if (type == null) {
			type = new JTextField();
			type.setBounds(new java.awt.Rectangle(164, 27, 151, 16));
			type.setText(tile.getType());
			type.addCaretListener(new CaretListener() {
				public void caretUpdate(CaretEvent e) {
					if (type.getText().length() <= 0) {
						type.setBackground(Color.PINK);
						ok.setEnabled(false);
					} else {
						type.setBackground(Color.WHITE);
						ok.setEnabled(true);
					}
				}
			});
		}
		return type;
	}

	/**
	 * This method initializes ok
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOk() {
		if (ok == null) {
			ok = new JButton();
			ok.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//tile.tile.type = tile.type = type.getText();
					tile.setType(type.getText());
					setVisible(false);
				}
			});
			ok.setBounds(new java.awt.Rectangle(107, 61, 71, 21));
			ok.setText("OK");
		}
		return ok;
	}

	/**
	 * This method initializes cancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton();
			cancel.setBounds(new java.awt.Rectangle(197, 61, 73, 21));
			cancel.setText("Cancel");
			cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});

		}
		return cancel;
	}
	
	class PImage extends JPanel {
		
		private static final long serialVersionUID = 9100845695599114700L;

		public void paint(Graphics g) {
			g.drawImage(tile.img, 0, 0, 64,64, this);
		}
		
	}

} // @jve:decl-index=0:visual-constraint="10,10"
